#include <iostream>
#include <iterator>
#include <ostream>
#include <vector>
#include <queue>
using namespace std;
struct node {
    int l, r;
    node(int x, int y) : l(x), r(y) {}
};
vector<node> a;
int main() {
    int n;
    cin >> n;
    a.push_back(node(-1, -1));
    for(int i = 0; i < n; i++) {
        int x, y;
        cin >> x >> y;
        a.push_back(node(x, y));
    }
    int k = 1;
    queue<node> q;
    q.push(a[1]);
    while(!q.empty()) {
        queue<node> t;
        while(!q.empty()) 
        {
            node temp = q.front();
            q.pop();
            if(temp.l)
                t.push(a[temp.l]);
            if(temp.r)
                t.push(a[temp.r]);
        }
        if(t.empty()) {
            cout << k << endl;
            return 0;
        }
        k++;
        q = t;
    }
    return 0;
}